local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local EventList = ServerOHOH.load.EventList();
local MinDt=ServerOHOH.config.Game.ActionSynchronizeTime


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.SynchronizeCD=MinDt/2
    self.LastTime=os.clock()-10 --上次同步实际

end;

function M.dtor(self)
	M.super.dtor(self);
end;


function M.FindClassFile(self,className)

    local file=ServerOHOH.load[className]
    if not file then
        file=_G.CommonOHOH.load[className]
    end
    return file
end

function M.FindComponent(self,file,isCharacter)

    if isCharacter then
        if self.parent.character then
            return   self.parent.character:GetComponent(file())
        end
    else
       return   self.parent:GetComponent(file())
    end
  
end


function M.DealAction(self,data)
    local FucData={}
    for _,v in pairs(data) do
        local fuc=nil
        local obj=nil
        if v.class then
            local file=self:FindClassFile(v.class)
            local ctrl=self:FindComponent(file,v.isCharacter)
            if not ctrl then
                warn("DealAction ctrl ==nil :---class, isCharacter--->",v.class,v.isCharacter,file)
                continue
            end
           
             fuc=ctrl[v.actionId]
             obj=ctrl
        else

             fuc=self[v.actionId]
             obj=self
        end

        if fuc then
            if not FucData[fuc] then
                FucData[fuc]={}
            end
            -- table.insert(FucData[fuc],{v.arg})
            table.insert(FucData[fuc],{
                a=obj,
                b=v.arg,
            })

        end
    end
    
    for fuc,list in pairs(FucData) do
        for _,arg in pairs(list) do
            fuc(arg.a,arg.b)
        end
        
    end

end

function M.GetSynchronizeCD(self)
    return  self.SynchronizeCD
end
function M.ActionSynchronize(self,data)
--    local endTime= self.LastTime+self:GetSynchronizeCD()
--     if os.clock()>endTime then
--         self.LastTime=endTime
--         self:DealAction(data)
--     else
--         warn("同步数据cd非法!")
--     end

        self:DealAction(data)
end

M:RegistEvent(EventList.action_synchronize,M.ActionSynchronize)


return M